package com.example.springboot_demo.controller;

import com.alibaba.fastjson2.JSON;
import com.example.springboot_demo.pojo.Book;
import com.example.springboot_demo.pojo.Cart;
import com.example.springboot_demo.pojo.Page;
import com.example.springboot_demo.service.BookService;
import com.example.springboot_demo.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
public class BookController {
    @Autowired
    private BookService bookService;

    //每页展示数据的常量
    private static final int pageSize = 4;


    //根据书本id查询
    @RequestMapping(value = "/getBookById", method = RequestMethod.GET)
    public Book getBookById(int bookId) {
        return bookService.getBookById(bookId);
    }


    //含有名字发分页查询
    @RequestMapping(value = "/getBooksByName", method = RequestMethod.GET)
    public Page<Book> getBooksByName(HttpServletRequest request, HttpServletResponse response) {
        int pageNo = WebUtils.ParseInt(request.getParameter("pageNo"), 1);
        String queryName = request.getParameter("queryName");
        return bookService.queryBooks(queryName, pageNo, pageSize);
    }

    @RequestMapping(value = "/getAllBooksByName", method = RequestMethod.GET)
    public List<Book> getAllBooksByName(HttpServletRequest request, HttpServletResponse response) {
        return bookService.getAllBooksByName(request.getParameter("queryName"));
    }

    //获取所有书籍
    @RequestMapping(value = "/getAllBooks", method = RequestMethod.GET)
    public List<Book> getAllBooks(HttpServletRequest request, HttpServletResponse response) {
        return bookService.getAllBooks();
    }

    //更新图书
    @RequestMapping(value = "/updateBook", method = RequestMethod.POST)
    public void updateBook(HttpServletRequest request, HttpServletResponse response) {
        //Webutils是工具类，将拿取的数据封装成对象
        bookService.updateBook(WebUtils.copyParamToBean(request.getParameterMap(), new Book()));
        //重定向回上一级
        try {
            response.sendRedirect(request.getHeader("Referer"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }



    //删除图书根据bookId
    @RequestMapping(value = "/deleteBook", method = RequestMethod.GET)
    public void deleteBook(HttpServletRequest request, HttpServletResponse response) {
        bookService.deleteBook(Integer.parseInt(request.getParameter("bookId")));
        //重定向回上一级
        try {
            response.sendRedirect(request.getHeader("Referer"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @RequestMapping(value = "/updateDataByCart", method = RequestMethod.POST)
    public void updateDataByCart(@RequestBody Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) {
        Cart<Book> cart = JSON.parseObject(JSON.toJSONString(map.get("cart")), Cart.class);
        bookService.updateDataByCart(cart);
        System.out.println(cart);
    }

}
